import axios from './axios'
import qs from 'qs'
import {baseUrl} from '@/config/config'

/**
 * 用户列表
 * @param {pageSize, pageIndex, keyword} params
 */
export const getSysuser = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/user?' + qs.stringify(params),
            method: 'get'
        }).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
}
/**
 * 用户详情
 * @param {name, phone, roleId, state, authority} params
 */
export const getSysuserDetail = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/user/' + params.userId,
            method: 'put',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            data: JSON.stringify(params)
        }).then(res => {
            res.data = {
                'code': 1,
                'msg': '处理成功',
                'data': {
                  'userId': 1,
                  'name': '张三',
                  'phone': '15980256438',
                  'state': 1,
                  'roleId': 2,
                  'roleName': 'admin',
                  'authority': [
                      {'code': '运营报表', 'permissions': ''},
                      {'code': '用户管理', 'permissions': 'view'},
                      {'code': '订购管理', 'permissions': 'view,edit'}
                  ]

                }
            }
            resolve(res.data)
        })
    })
}
/**
 * 用户编辑
 * @param {name, phone, roleId, state, authority} params
 */
export const editSysuser = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/user/' + params.userId,
            method: 'put',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            data: JSON.stringify(params)
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 用户新增
 * @param {name, phone, roleId, state, authority} params
 */
export const addSysuser = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/user',
            method: 'post',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            data: JSON.stringify(params)
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 用户删除
 * @param {id} params
 */
export const deleteSysuser = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/user/' + params.userId,
            method: 'delete'
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 密码重置
 * @param {newps} params
 */
export const resetPassword = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/user/ps/' + params.userId,
            method: 'put',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            data: JSON.stringify(params)
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 角色列表
 * @param {} params
 */
export const getSysroles = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/sysroles',
            method: 'get'
        }).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
}
/**
 * 角色详情
 * @param {roleId} params
 */
export const getSysrolesDetail = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            // url: baseUrl + '/api/sysroles/{roleId}',
            url: baseUrl + '/api/sysroles/' + params.roleId,
            method: 'get'
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 角色编辑
 * @param {roleId, roleName, describe, authority} params
 */
export const editSysroles = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            // url: baseUrl + '/api/sysroles/{roleId}',
            url: baseUrl + '/api/sysroles/' + params.roleId,
            method: 'put',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            data: JSON.stringify(params)
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 角色新增
 * @param {roleName, describe, authority} params
 */
export const addSysroles = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/sysroles',
            method: 'post',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            data: JSON.stringify(params)
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 角色删除
 * @param {roleId} params
 */
export const deleteSysroles = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            // url: baseUrl + '/api/sysroles/{roleId}',
            url: baseUrl + '/api/sysroles/' + params.roleId,
            method: 'delete'
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 用户权限
 * @param {userId} params
 */
export const getUserAuth = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/sysuser/' + params.userId,
            method: 'get'
        }).then(res => {
            resolve(res.data)
        })
    })
}

/**
 * 用户名是否存在
 * @param {userId} params
 */
export const checkSysuser = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/check/user/' + params.userName,
            method: 'get'
        }).then(res => {
            resolve(res.data)
        })
    })
}
/**
 * 角色名是否存在
 * @param {userId} params
 */
export const checkSysroles = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/check/sysroles/' + params.roleName,
            method: 'get'
        }).then(res => {
            resolve(res.data)
        })
    })
}

/**
 * @name: 解锁账号
 * @param {type}
 */
export const unlockAccount = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/user/unlock/' + params.userName,
            method: 'get'
        }).then(res => {
            resolve(res.data)
        })
    })
}

/**
 * @name: 代理人管辖范围
 * @param {type}
 */
 export const underCustId = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/get/agent/user/hover',
            method: 'get'
        }).then(res => {
            resolve(res.data)
        })
    })
}

/**
 * @name: 角色编辑范围
 * @param {type}
 */
 export const roleEditorDetail = (roleId) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/role/editor/' + roleId,
            method: 'get'
        }).then(res => {
            resolve(res.data)
        })
    })
}

/**
 * @name: 角色编辑范围新增
 * @param {type}
 */
 export const saveRoleEditor = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: baseUrl + '/api/role/editor',
            method: 'POST',
            data: params
        }).then(res => {
            resolve(res.data)
        })
    })
}
